<template>
  <div class="outer">
    <!-- <div class="header">
      <el-image src="./images/name.png"></el-image>
    </div>-->
    <div class="login-wrapper">
      <el-image src="./images/name.png"></el-image>
      <div class="text">
        Easy出行 轻松出行 你身边的出行管家
      </div>
      <div class="form-wrapper">
        <div class="form-head">管理员登陆</div>

        <el-form
          :model="ruleForm2"
          status-icon
          :rules="rules2"
          ref="ruleForm2"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item prop="usernsme">
            <el-input v-model.number="ruleForm2.usernsme" placeholder="管理员账号"></el-input>
          </el-form-item>
          <el-form-item prop="pass">
            <el-input v-model="ruleForm2.pass" auto-complete="off" placeholder="密码"></el-input>
          </el-form-item>
          <el-form-item prop="checkPass">
            <el-input v-model="ruleForm2.checkPass" auto-complete="off" placeholder="确认密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button class="login" @click="submitForm('ruleForm2')">登录</el-button>
            <!-- <el-button @click="resetForm('ruleForm2')">重置</el-button> -->
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    var checkUsernsme = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("账号不能为空"));
      }
      setTimeout(() => {
        var ab = /[a-zA-Z]{4,}/;
        if (ab.test(value) == false) {
          callback(new Error("账号错误!"));
        } else {
          callback();
        }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        var pa = /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,10}/;
        if (pa.test(value) == false) {
          callback(new Error("密码必须包含字母、数字和特殊字符!"));
        } else {
          if (this.ruleForm2.checkPass !== "") {
            this.$refs.ruleForm2.validateField("checkPass");
          }
          callback();
        }
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm2.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm2: {
        pass: "",
        checkPass: "",
        usernsme: ""
      },
      rules2: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        usernsme: [{ validator: checkUsernsme, trigger: "blur" }]
      }
    };
  },
  created() {},
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.login();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    // 发送请求
    async login() {
      // 发送请求
      const params = {
        username: this.ruleForm2.usernsme,
        password: this.ruleForm2.pass
      };
      console.log(params);
      const { data } = await axios.get("/adm/login", { params });
      console.log(data);
      if (data.success) {
        localStorage.setItem("token", data.token);
        // this.$message({
        //   showClose: true,
        //   message: "登录成功！",
        //   type: "success"
        // });
        this.$notify({
          title: "成功",
          message: "登录成功!",
          type: "success"
        });
        this.$router.push("/AdmSystem");
      } else {
        this.$notify.error({
          title: "错误",
          message: "用户名或密码错误！"
        });
      }
    }
  }
};
</script>

<style scoped>
/* .outer{
  display: flex;
  flex-direction: column;
} */
/* .header {
  height: 15vh;
  background-color: black;
  display: flex;
  align-items: center;
} */
.header .el-image {
  width: 120px;
  height: 50px;
  margin-left: 40px;
}
.login-wrapper {
  width: 100vw;
  height: 100vh;
  background-image: url("../assets/bg1.jpg");
  background-position: center -10px;
  background-repeat: no-repeat;
  background-size: cover;
}
.form-wrapper {
  width: 450px;
  position: fixed;
  top: 150px;
  right: 200px;
  background-color: rgb(255, 255, 255);
}
.el-form {
  margin: 0 100px 40px 0;
}
.form-head {
  height: 80px;
  width: 100%;
  text-align: center;
  line-height: 80px;
  font-size: 18px;
}
.el-form-item__content {
  margin-left: 0 !important;
}
label {
  text-align: left !important;
  display: flex !important;
}
.el-form-item__label {
  color: white !important;
}
.change-label-calss /deep/ .el-form-item__label {
  color: white;
}
.el-input {
  opacity: 0.8;
}
.el-input__inner {
  border: none;
}
.el-button {
  border: none;
}
.login {
  background-color: rgb(92, 119, 182);
  color: white;
  width: 100%;
}
.login:hover {
  background-color: rgb(94, 117, 172);
}
.el-image {
  margin: 50px 0 0 50px;
  width: 300px;
}
.text {
  margin-left: 80px;
  width: 400px;
  line-height: 30px;
  color: white;
}
</style>